package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.com.kunpeng.entity.OaCustomer;
import org.jeecg.modules.demo.com.kunpeng.mapper.OaCustomerMapper;
import org.jeecg.modules.demo.com.kunpeng.service.IOaCustomerService;
import org.jeecg.modules.demo.com.kunpeng.vo.OaCustomerContactListByShareIdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class OaCustomerServiceImpl extends ServiceImpl<OaCustomerMapper, OaCustomer> implements IOaCustomerService {

    @Autowired
    private OaCustomerMapper oaCustomerMapper;

    /**
     * 根据共享id获取客户列表
     */
    @Override
    public List<OaCustomerContactListByShareIdVO> OaCustomerListByShareIds(String shareId) {
        List<OaCustomer> oaCustomers = oaCustomerMapper.oaCustomerListByShareIds(shareId);

        List<OaCustomerContactListByShareIdVO> newOaCustomerList=new ArrayList<>();

        for (OaCustomer oaCustomer:oaCustomers) {
            OaCustomerContactListByShareIdVO oaCustomerListByShareIdVO=new OaCustomerContactListByShareIdVO();
            /*System.out.println("数据库获取的oaCustomer："+oaCustomer.toString());*/
            String[] split = oaCustomer.getShareIds().split(",");
            if(split.length>1){
                for(int i=0;i<split.length;i++){
                    if(split[i].equals(shareId)){
                        oaCustomerListByShareIdVO.setId(oaCustomer.getId());
                        oaCustomerListByShareIdVO.setName(oaCustomer.getName());
                        newOaCustomerList.add(oaCustomerListByShareIdVO);
                    }
                }
            }else {
                oaCustomerListByShareIdVO.setId(oaCustomer.getId());
                oaCustomerListByShareIdVO.setName(oaCustomer.getName());
                newOaCustomerList.add(oaCustomerListByShareIdVO);
            }
        }
        return newOaCustomerList;
    }


    /**
     * 获取所以客户列表
     * @return
     */
    @Override
    public List<OaCustomer> oaCustomerList() {
        List<OaCustomer> oaCustomers = oaCustomerMapper.oaCustomerList();
        return oaCustomers;
    }

    /**
     * 根据客户id获取客户信息
     */
    @Override
    public OaCustomer selectOaCustomerById(Integer id) {
        OaCustomer oaCustomer = oaCustomerMapper.selectOaCustomerById(id);
        return oaCustomer;
    }
}
